import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import AdminView from '../views/AdminView.vue'
import LoginView from "@/views/LoginView.vue";
import LayoutView from "@/views/LayoutView.vue";
import RegisterView from "@/views/RegisterView.vue";
import BookView from "@/views/BookView.vue";
import TypeView from "@/views/TypeView.vue";
import AuditView from "@/views/AuditView.vue";
import HotelView from "@/views/HotelView.vue";
import ReserveView from "@/views/ReserveView.vue";
import LogView from "@/views/LogView.vue";
import NoticeView from "@/views/NoticeView.vue";

Vue.use(VueRouter)

const routes = [
    // 一级路由：注册
  {
    path: '/register',
    name: 'register',
    component: RegisterView
  },


    // 一级路由：登录
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },


    // 一级路由：系统
  {
    path: '/',
    name: 'layout',
    component: LayoutView,
    children: [  //子路由
      {
        path: '',
        name: 'home',
        component: HomeView
      },
      {
        path: 'admin',
        name: 'admin',
        component: AdminView
      },
       // 图书的路由
      {
        path: 'book',
        name: 'book',
        component: BookView
      },

        //图书分类
      {
        path: 'type',
        name: 'type',
        component: TypeView
      },
        //请假审核
      {
        path: 'audit',
        name: 'audit',
        component: AuditView
      },
        //酒店信息
      {
         path: 'hotel',
         name: 'hotel',
         component: HotelView
      },
        //预定酒店
      {
         path: 'reserve',
         name: 'reserve',
         component: ReserveView
      },
        //操作日志
      {
         path: 'log',
         name: 'log',
         component: LogView
      },
      // 系统通知
      {
        path: 'notice',
        name: 'notice',
        component: NoticeView
      },
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 前端路由守卫
router.beforeEach((to,from,next) => {
  // if (to.path === '/login'){
  //   next();
  // }
  const user = localStorage.getItem('user');
  if (!user && to.path !== '/login' && to.path !== '/register'){
    return next('/login');
  }
  next();
})

export default router
